<?php defined('BASEPATH') OR exit('No direct script access allowed');

class setLanguage extends wsBackend
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('language_model');
    }

    public function index()
    {
        $data = array();
        $this->tpl->meta ['title'] = '选择语言 - ' . $this->config->item('site_name');
        $this->tpl->meta ['description'] = '';
        $this->tpl->meta ['keywords'] = '';
        $this->tpl->page_header = '选择语言';
        $this->tpl->page_header_ = 'Language Panel';
        $this->tpl->breadcrumbs['SetLanguage'] = 'active';

        $data['languages'] = $this->language_model->getAll_languages();
        $data['dl'] = $this->language_model->get_defaultLanguages(false);

        $this->tpl->contents [] = $this->tpl->fetch('contents/SetLanguage', $data);
        $this->tpl->render();
    }


    public function activate($grouping_id = '', $language_id = '')
    {
        if (!$grouping_id || !$language_id) {
            $this->session->set_flashdata('ERROR', 'Required A valid ID');
            redirect(admin_url('setLanguage'));
        }

        $Language_exists = $this->language_model->get_languageById($language_id);
        if (!$Language_exists) {
            $this->session->set_flashdata('ERROR', $language_id . ' Is not exists in the database');
            redirect(admin_url('setLanguage'));
        }
        $this->load->model('routes_model');
        $this->language_model->activate_grouping_language($grouping_id, $language_id);
        $this->load->model('metadata_model');
        //Fix Missing language MetaKeys of the selected language
        $this->metadata_model->updateMissingKeys($grouping_id, $language_id);
        $this->routes_model->updateMissingKeys($grouping_id, $language_id);
        $this->metadata_model->updateMissingTextKeys($grouping_id, $language_id);

        $this->language->onchangeLanguage();

        $this->session->set_flashdata('SUCCESS', 'Language Activated Successfully to the group Frontend');
        redirect(admin_url('setLanguage'));
    }
}
